package com.hyy.customer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyy.api.domain.customer.CstmCorporate;
import com.hyy.api.domain.customer.bo.CstmCorporateBo;
import com.hyy.api.domain.customer.vo.CstmCorporateVo;
import com.hyy.api.domain.system.bo.SysUserBo;
import com.hyy.common.core.utils.MapstructUtils;
import com.hyy.common.core.utils.StringUtils;
import com.hyy.common.mybatis.core.page.PageQuery;
import com.hyy.common.mybatis.core.page.TableDataInfo;
import com.hyy.common.redis.utils.RedisUtils;
import com.hyy.customer.mapper.CstmCorporateMapper;
import com.hyy.customer.service.ICstmCorporateService;
import com.hyy.customer.service.IModCstmSysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import static com.hyy.api.constant.BusinessRedisConstants.CSTM_CORPORATE_START;

/**
 * 下游客户公司Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-16
 */
@RequiredArgsConstructor
@Service
public class CstmCorporateServiceImpl implements ICstmCorporateService {

    private final CstmCorporateMapper baseMapper;
    private final IModCstmSysUserService sysUserService;

    /**
     * 查询下游客户公司
     *
     * @param id 主键
     * @return 下游客户公司
     */
    @Override
    public CstmCorporateVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询下游客户公司列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 下游客户公司分页列表
     */
    @Override
    public TableDataInfo<CstmCorporateVo> queryPageList(CstmCorporateBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CstmCorporate> lqw = buildQueryWrapper(bo);
        Page<CstmCorporateVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的下游客户公司列表
     *
     * @param bo 查询条件
     * @return 下游客户公司列表
     */
    @Override
    public List<CstmCorporateVo> queryList(CstmCorporateBo bo) {
        LambdaQueryWrapper<CstmCorporate> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CstmCorporate> buildQueryWrapper(CstmCorporateBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CstmCorporate> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(CstmCorporate::getId);
        lqw.like(StringUtils.isNotBlank(bo.getName()), CstmCorporate::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getCode()), CstmCorporate::getCode, bo.getCode());
        lqw.eq(bo.getStatus() != null, CstmCorporate::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getPwd()), CstmCorporate::getPwd, bo.getPwd());
        return lqw;
    }

    /**
     * 新增下游客户公司
     *
     * @param bo 下游客户公司
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(CstmCorporateBo bo) {
        CstmCorporate add = MapstructUtils.convert(bo, CstmCorporate.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
            //新增sys_user表
            SysUserBo sysUserBo = new SysUserBo();
            sysUserBo.setUserName(bo.getCode());
            sysUserBo.setPassword(bo.getPwd());
            sysUserBo.setNickName(bo.getName());
            sysUserBo.setStatus("0");
            sysUserService.insertCorporateUser(sysUserBo);
        }

        return flag;
    }

    /**
     * 修改下游客户公司
     *
     * @param bo 下游客户公司
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(CstmCorporateBo bo) {
        CstmCorporate update = MapstructUtils.convert(bo, CstmCorporate.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CstmCorporate entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除下游客户公司信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public CstmCorporateVo newCorporate() {

        Integer cstm_corporate_start = RedisUtils.getCacheObject(CSTM_CORPORATE_START);
        if(cstm_corporate_start == null){
            RedisUtils.setCacheObject(CSTM_CORPORATE_START, 100000);
        }


        long account = RedisUtils.incrAtomicValue(CSTM_CORPORATE_START);
        CstmCorporateVo cstmCorporateVo = new CstmCorporateVo();
        cstmCorporateVo.setCode(account+"");
        String key = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 8);
        cstmCorporateVo.setPwd(key);
        cstmCorporateVo.setStatus(1);

        return cstmCorporateVo;
    }
}
